package com.zx.video28181.util;

public class MakeHeaderUtil {

    private final int PS_HDR_LEN = 14;
    private final int SYS_HDR_LEN = 18;
    private final int PSM_HDR_LEN = 24;
    private final int PES_HDR_LEN = 14;
    private final int RTP_HDR_LEN = 12;
    private final int RTP_VERSION = 2;
    private final int RTP_MAX_PACKET_BUFF = 1460;
    void bitsWrite(BufferData dat,int count, byte bit)
    {
        if (count > 0)
        {
//            count--;
//            if ((bit >>count) & 0x01) {
//
//            }
        }
    }
    // RTP

    /**
     * @param seq  序号
     * @param timestamp 时间戳
     * @param ssrc 0X00
     * @param byteTag M_PT标识
     * @return
     */
    public  byte[] getRtpHeader(int seq,int timestamp,int ssrc,byte byteTag){

        byte[] headerArr = new byte[12];
        //字节1:|V2|P1|X1|CC4   |
        headerArr[0] = (byte) 0x80;//10000000==>V=2,P=0,X=0,CC=0000
        //字节2:|M1|     PT7    |
        headerArr[1] = byteTag;//01100000==>M=0,PT=1100000
        //字节3-4:|sequence number16|：headerArr[2],headerArr[3]
        System.arraycopy(intToBytes(seq,2),0,headerArr,2,2);
        //字节5-8|timestamp时间戳|：headerArr[4]~headerArr[7]
        System.arraycopy(intToBytes(timestamp,4),0,headerArr,4,4);
        //字节9-12|同步信源(SSRC)标识符|：headerArr[8]~headerArr[11]
        System.arraycopy(intToBytes(ssrc,4),0,headerArr,8,4);
        return headerArr;
    }


    /**
     *   将32位长度转换为n字节。（大端字节序：高位在前，低位在后）
     *   @param ldata 将从中构造n字节数组的int。
     *   @param n 要将长文件转换为的所需字节数。
     *   @return 用长值填充的所需字节数组。
     */
    public byte[] intToBytes(int ldata, int n) {
        byte[] buff = new byte[n];
        for (int i=n-1;i>=0;i--) {
            // 保持将最右边的8位分配给字节数组，同时在每次迭代中移位8位
            buff[i] = (byte)ldata;
            ldata = ldata>>8;
        }
        return buff;
    }

    /**
     * int到byte[] 由高位到低位
     * @param i 需要转换为byte数组的整行值。
     * @return byte数组
     */
    public static byte[] intToByteArray(int i) {
        byte[] result = new byte[4];
        result[0] = (byte)((i >> 24) & 0xFF);
        result[1] = (byte)((i >> 16) & 0xFF);
        result[2] = (byte)((i >> 8) & 0xFF);
        result[3] = (byte)(i & 0xFF);
        return result;
    }
    // PS  PS Header + SYS Header + PSM Header + PES Header + PES packet * N
    public  byte[] makePSHeader(){
        byte[] pshbyte = new byte[14];
        BufferData bufferData = new BufferData();
        System.arraycopy(intToBytes(0x000001BA,4),0,pshbyte,0,4); // startcode



        return  pshbyte;

    }

    public  byte[] makeSYSHeader(){
        byte[] sysbyte = new byte[16];

        return sysbyte;
    }

    public  byte[] makePSMHeader(){


        return null;
    }

    public  byte[] makePESHeader(){

        return null;
    }


    public class BufferData{
        char data; // 真实数据
        char mask;
        int size;
        int iData;
        public BufferData(){

        }
        public BufferData(char d,char m,int s, int id){
            data = d;
            mask = m;
            size = s;
            iData = id;
        }
        public void  setData(char c)
        {
            data = c;
        }
        public void  setMask(char c)
        {
            mask = c;
        }
        public void  setSize(int c)
        {
            size = c;
        }
        public void  setiData(int c)
        {
            iData = c;
        }

        public char getData(){
            return  data;
        }
        public char getMask(){
            return  mask;
        }
        public int getSize(){
            return  size;
        }
        public int getiData(){
            return  iData;
        }

    }
}
